﻿/**
 * jQuery.bottom
 * Dual licensed under MIT and GPL.
 * Date: 2010-04-25
 *
 * @description Trigger the bottom event when the user has scrolled to the bottom of an element
 * @author Jim Yi
 * @version 1.0
 *
 * @id jQuery.fn.bottom
 * @param {Object} settings Hash of settings.
 * @return {jQuery} Returns the same jQuery object for chaining.
 *
 */
(function ($) {
    $.fn.bottom = function (options) {

        var defaults = {
            // how close to the scrollbar is to the bottom before triggering the event
            proximity: 0
        };

        var options = $.extend(defaults, options);

        return this.each(function () {
            var obj = this;
            $(obj).bind("scroll", function () {
                if (obj == window) {
                    scrollHeight = $(document).height();
                }
                else {
                    scrollHeight = $(obj)[0].scrollHeight;
                }
                scrollPosition = $(obj).height() + $(obj).scrollTop();
                if ((scrollHeight - scrollPosition) / scrollHeight <= options.proximity) {
                    $(obj).trigger("bottom");
                }
            });

            return false;
        });
    };
})(jQuery);